<?php

namespace App\Http\Controllers\Backend;


use App\Manage\CourseManage;
use App\Manage\CourseStudentManage;
use App\Models\Admin;
use App\Models\Course;
use App\Models\SiteInfo;
use App\Models\Student;
use App\Models\User;
use App\Models\UserAuth;

class CourseStudentController extends BaseController
{

    // 课程学员
    public function initialize()
    {
        parent::initialize();

        $this->model = 'CourseStudent';
    }


    // 列表
    // 添加
    // 修改
    // 删除


    // 返回列表数据
    public function lists()
    {
        $manage = new CourseStudentManage();
        $result = $manage->onPageLists();
        if($result === false){
            return $this->error($manage->getError());
        }

        // 获取用户信息
        $uids  = array_column($result['lists'], 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);
        // 课程信息
        $cids    = array_column($result['lists'], 'course_id');
        $courses = (new Course())->isCache(true)->getListInIds($cids);
        // 学员信息
        $sids     = array_column($result['lists'], 'student_id');
        $students = (new Student())->isCache(true)->getListInIds($sids);
        foreach($result['lists'] as $key => $item){
            $result['lists'][$key]['user']    = $users[$item['uid']] ?? [];
            $result['lists'][$key]['student'] = $students[$item['student_id']] ?? [];
            $result['lists'][$key]['course']  = $courses[$item['course_id']] ?? [];
        }

        return $this->success($result);
    }


    // 详情：修改的时候要使用的
    public function show()
    {
        $manage = new CourseStudentManage();
        $info   = $manage->onShow();
        if($info === false){
            return $this->error($manage->getError());
        }

        // 获取用户信息
        $info['user']    = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);
        $info['student'] = (new Student())->isCache(true)->getOneById($info['student_id'] ?? 0);
        $info['course']  = (new Course())->isCache(true)->getOneById($info['course_id'] ?? 0);

        return $this->success($info);
    }


}


